<template>
  <div class="login-bg">
    <div id="login-container">
      <h2 style="text-align: center; margin-top: 0">图书借阅系统</h2>
      <el-form
        ref="form"
        :model="form"
        @submit.native.prevent="handleRegister"
        :rules="rules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            clearable
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            clearable
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            placeholder="请再次输入密码"
            clearable
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            clearable
            prefix-icon="el-icon-phone"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            style="
              width: 100%;
              height: 40px;
              display: flex;
              align-items: center;
              justify-content: center;
            "
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <div style="text-align: center; margin-top: 10px">
        已有账号？<router-link to="/login">直接登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "RegisterPage",
  data() {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    const validatePhone = (rule, value, callback) => {
      const phoneReg = /^1[3-9]\d{9}$/;
      if (!value) {
        return callback(new Error("请输入手机号"));
      }
      if (!phoneReg.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    };

    return {
      form: {
        username: "",
        password: "",
        confirmPassword: "",
        phone: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validateConfirmPassword, trigger: "blur" },
        ],
        phone: [{ validator: validatePhone, trigger: "blur" }],
      },
    };
  },
  methods: {
    handleRegister() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submit();
        }
      });
    },
    async submit() {
      try {
        const response = await this.$request.post(
          "/api/auth/register",
          this.form
        );
        if (response.code === 200) {
          this.$message.success("注册成功");
          this.$router.push("/login");
        } else {
          this.$message.error(response.message);
        }
      } catch (error) {
        this.$message.error(error.message || "注册失败");
      }
    },
  },
};
</script>

<style scoped>
.login-bg {
  background: url("@/assets/login.jpg") no-repeat center center/cover;
  z-index: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}

#login-container {
  z-index: 1;
  width: 400px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 15px;
}

.el-form-item__label {
  line-height: 32px;
}

.el-input__prefix {
  color: rgba(0, 0, 0, 0.25);
}
</style>